package pres.man.interview.collection;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * HashMap 不安全场景
 *
 * @Author MAN
 * @Date 2021-01-20-14:41
 */
public class UnSafeMap {

    private final static Map<Integer, Integer> MAP = new HashMap<>(16);

    public static void main(String[] args) throws InterruptedException {
        ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(8, 8, 3, TimeUnit.SECONDS, new ArrayBlockingQueue<>(10), new ThreadPoolExecutor.CallerRunsPolicy());

        AtomicInteger atomicInteger = new AtomicInteger(0);
        for (int i = 0; i < 100; i++) {
            poolExecutor.execute(() -> MAP.put(atomicInteger.incrementAndGet(), atomicInteger.get()));
            poolExecutor.execute(() -> MAP.put(atomicInteger.incrementAndGet(), atomicInteger.get()));
            poolExecutor.execute(() -> MAP.get(atomicInteger.get()));
            poolExecutor.execute(() -> MAP.get(atomicInteger.get()));
            poolExecutor.execute(() -> MAP.get(atomicInteger.get()));
            poolExecutor.execute(() -> MAP.remove(atomicInteger.get()));
        }

        TimeUnit.SECONDS.sleep(20L);

        if(MAP.containsValue(null)){
            System.out.println("oh yeah");
        }
        
    }
}
